package stream;

import stream.pojo.Trander;
import stream.pojo.Transaction;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.toList;

/**
 * 〈java8 example〉<br>
 *
 * @author hedajiang
 * @create 2017/11/26
 * @since 1.0.0
 */
public class TransTest {
    public static void main(String[] args) {
        Trander raoul = new Trander("Raoul","Cambridge");
        Trander mario = new Trander("Mario","Milan");
        Trander alan = new Trander("Alan","Cambridge");
        Trander brian = new Trander("Brian","Cambridge");

        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian,2011,300),
                new Transaction(raoul,2012,1000),
                new Transaction(raoul,2011,400),
                new Transaction(mario,2012,710),
                new Transaction(mario,2012,700),
                new Transaction(alan,2012,950)

        );

        //1、获取2011年的交易，并且按照量的大小排序（从小到大）,如何从大到小？
        transactions.stream()
                .filter(transaction ->  transaction.getYear()==2011)
                .sorted(Comparator.comparing(transaction->transaction.getValue()))
                .collect(toList())
                .forEach(System.out::println);

        //2、获取交易员的工作城市
        transactions.stream()
                .map(transaction -> transaction.getTrander().getCity())
                .distinct()
                //.sorted((c1,c2)->c2.compareTo(c1))
                .forEach(System.out::println);

        //3、获取所有来自Cambridge的交易员，并按照name排序输出
        transactions.stream()
                .map(Transaction::getTrander)
                .filter(trander -> trander.getCity().equals("Cambridge"))
                .distinct()
                .sorted(Comparator.comparing(trander -> trander.getName()))
                .collect(toList()).forEach(System.out::println);

        //4、获取所有交易员的名称，并且排序
        String names = transactions.stream().map(transaction -> transaction.getTrander().getName())
                .distinct()
                .sorted()
                .reduce("",(name1,name2)->name1+" "+name2);
        System.out.println(names);

        //5、又在米兰的交易员吗？
        boolean result1 = transactions.stream()
                .anyMatch(transaction -> transaction.getTrander().getCity().equals("Milan"));
        boolean result2 = transactions.stream()
                .map(Transaction::getTrander).anyMatch(trander -> trander.getCity().equals("Milan"));
        System.out.println(result1);
        System.out.println(result1==result2);

        //6、获取生活在剑桥交易员的交易金额
        transactions.stream().filter(transaction -> transaction.getTrander().getCity().equals("Cambridge"))
                .map(Transaction::getValue)
                .forEach(System.out::println);
        //.forEach(transaction -> System.out.println(transaction.getValue()));

        //7、获取交易中最大交易金额是多少
        Optional<Integer> maxValue = transactions.stream()
                .map(Transaction::getValue)
                .reduce(Integer::max);
        System.out.println("maxValue = "+maxValue.get());

        //
    }

}